﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Net;
using System.Threading;
using z_storm.suny.F_ALL;


namespace UpdataStart
{
    public static class Program
    {
        public const string UPDATER_EXE_NAME = "AutoUpdate.exe";
        public const string ReleaseConfigFileName = "ReleaseList.xml";

        private static ReleaseList localRelease;
        private static ReleaseList remoteRelease;
        private static string tempPath;
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            try
            {
                int li_ret;

                li_ret = OPVcDll.f_setupdatestart();
                if (li_ret < 0)
                {
                    return;
                }
                //更改主程序的配置文件
                configTool.GetConfige("MainUpdate", "false");
                //获取本地relealist文件信息
                string localXmlPath = string.Format("{0}\\{1}", Application.StartupPath, ReleaseConfigFileName);
                localRelease = new ReleaseList(localXmlPath);
                tempPath = Path.GetTempPath();   //系统临时文件夹
                           
                // 下载服务器上的relealist文件
                DownloadTool.DownloadFile(tempPath, localRelease.ReleaseUrl, ReleaseConfigFileName);
                //把relealist.xml文件下载到本地后，从本地读取
                remoteRelease = new ReleaseList(tempPath + ReleaseConfigFileName);

                //比较本机文件和服务器上的XML文件
                if (localRelease.Compare(remoteRelease) != 0)
                {
                    //if (CheckProcessing() != DialogResult.OK)
                    //{
                    //    AppTool.DeleteTempFolder(tempPath);
                    //    Application.Exit();
                    //    return;
                    //}
                    UpdateForm form = new UpdateForm(tempPath, localRelease, remoteRelease);
                    Application.Run(form);
                }
                else
                {
                    AppTool.DeleteTempFolder(tempPath); //删除系统目录 C:\Documents and Settings\Administrator\Local Settings\Temp\
                    Application.Exit();
                    //AppTool.Start(localRelease.ApplicationStart);
                     Process.Start("suny.exe");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Application.Exit();
                //AppTool.Start(localRelease.ApplicationStart);
                Process.Start("suny.exe");
                return;
            }         
        }

        /// <summary>
        /// 判断现在是否有主项目在运行
        /// </summary>
        /// <returns></returns>
        //static DialogResult CheckProcessing()
        //{
        //    string exeName = localRelease.ApplicationStart.Substring(0, localRelease.ApplicationStart.Length - 4);
        //    if (Process.GetProcessesByName(exeName).Length > 0)
        //    {
        //        var rs = MessageBox.Show(string.Format("请先退出正在运行的{0}", exeName), "警告", MessageBoxButtons.RetryCancel,
        //                                 MessageBoxIcon.Warning,
        //                                 MessageBoxDefaultButton.Button1);
        //        if (rs == DialogResult.Retry)
        //        {
        //            return CheckProcessing();
        //        }
        //        return rs;
        //    }
        //    return DialogResult.OK;
        //}

        //// 全局异常处理
        //public class ApplicationEventHandlerClass
        //{
        //    public void OnThreadException(object sender, ThreadExceptionEventArgs e)
        //    {
        //       MessageBox.Show(e.Exception.Message); // 调用FormSysMessage窗体，显示异常信息。
        //    }
        //}
    }
}
